<!DOCTYPE html>
<html lang="zh-CN"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>微信支付</title>
    <!--<script src="/js/jquery.js"></script>-->
    <script th:src="@{/shoppingApi/js/jquery-3.3.1.min.js}"></script>
</head>
<body>

<div align="center">
    <br/><br/><br/>
    <input type="hidden" ID="submit" value="立即支付" onclick="callpay();" style="width:210px; height:50px; border-radius: 15px;background-color:#00CD00; border:0px #FE6714 solid; cursor: pointer;  color:white;  font-size:16px;" />
</div>

<input type="hidden" th:if="${JSAPI!=null}" th:data="${JSAPI.appId}" th:attr="value=${JSAPI.appId}" id="appId" name="appId" />
<input type="hidden" th:if="${JSAPI!=null}"  th:attr="value=${JSAPI.timeStamp}" id="timeStamp" name="timeStamp" />
<input type="hidden" th:if="${JSAPI!=null}"  th:attr="value=${JSAPI.package}" id="prepay_id" name="prepay_id" />
<input type="hidden" th:if="${JSAPI!=null}"  th:attr="value=${JSAPI.paySign}" id="paySign" name="paySign" />
<input type="hidden" th:if="${orderNo!=null}"  th:attr="value=${orderNo}" id="orderNo" name="orderNo" />
<script type="text/javascript">
    var orderNo=$.trim($("#orderNo").val());
    var appId =$.trim($("#appId").val());
$(function () {
    callpay();
});
    //调用微信JS api 支付
    function jsApiCall() {
        var timeStamp=$.trim($("#timeStamp").val());
        var prepay_id=$.trim($("#prepay_id").val());
        var paySign=$.trim($("#paySign").val());
        if(appId==""&&orderNo!=""){
            alert('支付失败');
            window.location.href="/wap/payOrder?No="+orderNo;
        }else if(appId==""&&orderNo==""){
            alert('支付失败');
            window.location.href='/wap/getOrderList?orderStatus=701';
        }
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            {
                "appId":appId,            //公众号名称，由商户传入
                "timeStamp":timeStamp,    //时间戳，自1970年以来的秒数
                "nonceStr":"15513fds",    //随机串
                "package":prepay_id,
                "signType":"MD5",         //微信签名方式：
                "paySign": paySign        //微信签名
            },
            function (res) {
                //WeixinJSBridge.log(res.err_msg);
                if(res.err_msg=='get_brand_wcpay_request:ok'){
                    if($("#orderNo").val()!=""){
                        $.ajax({
                            url: '/API/Weixin/orderCallback',
                            //type: 'post',
                            data: {
                                orderNo: $("#orderNo").val()
                            },
                            dataType: 'json',
                            contentType: 'application/json',
                            async: false,
                            success: function (data) {
                                //alert(JSON.stringify(data));
                                if (data.data>0) {
                                    // alert("支付成功!")
                                    window.location="/wap/getOrderDetails?orderNo="+data.errorDetail;
                                }
                                else {
                                    alert(data.errorDetail);
                                }
                            }
                        });
                    }else {
                        alert('支付失败，订单号有误!');
                    }
                }else{
                    alert('支付失败'+res.err_msg);//这里一直返回getBrandWCPayRequest提示fail_invalid appid
                    window.location="/wap/payOrder?No="+$("#orderNo").val();
                }
            }
        );
    }

    function callpay() {
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                if(appId==""&&orderNo!=""){
                    alert('支付失败');
                    window.location.href="/wap/payOrder?No="+orderNo;
                }else if(appId==""&&orderNo==""){
                    alert('支付失败');
                    window.location.href='/wap/getOrderList?orderStatus=701';
                }
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            }
            else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        }
        else {
            jsApiCall();
        }
    }

</script>
</body>
</html>